package com.bdbit.ChargingStation.mapper;

import com.bdbit.ChargingStation.entity.ChargingPile;
import com.bdbit.ChargingStation.entity.OrderEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChargingPileMapper {

    // 查询所有充电桩
    List<ChargingPile> selectAll();

    // 根据区域查询充电桩
    List<ChargingPile> selectByRegion(@Param("regionCode") String regionCode);

    // 根据区域和电桩号查询单个充电桩
    ChargingPile selectByRegionAndCode(
            @Param("regionCode") String regionCode,
            @Param("eleCode") String eleCode
    );

    // 新增充电桩
    @Insert("INSERT INTO charging_pile (region_code, region_name, ele_code, ele_name, status, ele_old_url, controller_old_url, type, create_time, update_time) " +
            "VALUES (#{regionCode}, #{regionName}, #{eleCode}, #{eleName}, #{status}, #{eleOldUrl}, #{controllerOldUrl}, #{type}, NOW(), NOW())")
    int insertChargingPile(ChargingPile pile);

    // 更新充电桩信息
    //int updateChargingPile(ChargingPile pile);
    @UpdateProvider(type = ChargingPileSqlBuilder.class, method = "buildUpdateSql")
    int updateChargingPile(ChargingPile pile);

    /**
     * 根据电桩号查询充电桩
     * @param eleCode 电桩号
     * @return 匹配的充电桩列表
     */
    @Select("SELECT * FROM charging_pile WHERE ele_code = #{eleCode}")
    List<ChargingPile> selectByEleCode(String eleCode);

    public class ChargingPileSqlBuilder {
        public String buildUpdateSql(ChargingPile pile) {
            String sql = "UPDATE charging_pile SET " +
                    "region_name = '" + pile.getRegionName() + "', " +
                    "ele_name = '" + pile.getEleName() + "', " +
                    //...其他字段
                    "WHERE region_code = '" + pile.getRegionCode() +
                    "' AND ele_code = '" + pile.getEleCode() + "'";
            System.out.println("DEBUG SQL: " + sql); // 控制台输出
            return sql;
        }
    }
    // 删除单个充电桩
    // ChargingPileMapper.java
    @Delete("DELETE FROM charging_pile WHERE region_code = #{regionCode} AND ele_code = #{eleCode}")
    int deleteChargingPile(@Param("regionCode") String regionCode,
                           @Param("eleCode") String eleCode);

    // 批量删除充电桩
    int batchDeleteChargingPile(@Param("list") List<ChargingPile> piles);

    List<OrderEntity> getChargingPilesOrder(@Param("eleCode") String eleCode);
}